<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>Simple Tools 管理系统</h2>
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
<!--        <el-form-item v-if="showCaptcha" prop="captcha">-->
<!--          <div class="captcha-container">-->
<!--            <el-input-->
<!--              v-model="loginForm.captcha"-->
<!--              placeholder="请输入验证码"-->
<!--              prefix-icon="Picture"-->
<!--            />-->
<!--            <img-->
<!--              :src="captchaImage"-->
<!--              class="captcha-image"-->
<!--              @click="refreshCaptcha"-->
<!--              alt="验证码"-->
<!--            />-->
<!--          </div>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, type FormInstance } from 'element-plus';
import { User, Lock, Picture } from '@element-plus/icons-vue';
import { useUserStore } from '@/store/user';
// import { getCaptcha } from '@/api/auth';

const router = useRouter();
const userStore = useUserStore();

// 登录表单
const loginFormRef = ref<FormInstance>();
const loginForm = reactive({
  username: '',
  password: '',
  captcha: '',
  captchaId: '',
  rememberMe: false
});

// 表单校验规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
};

// 状态
const loading = ref(false);
const showCaptcha = ref(false);
const captchaImage = ref('');

// 刷新验证码
const refreshCaptcha = async () => {
  try {
    // const res = await getCaptcha();
    // if (res.data) {
    //   loginForm.captchaId = res.data.captchaId;
    //   captchaImage.value = res.data.captchaImage;
    // }
  } catch (error) {
    console.error('获取验证码失败', error);
  }
};

// 登录
const handleLogin = async () => {
  if (!loginFormRef.value) return;

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        const success = await userStore.login(loginForm.username, loginForm.password);
        if (success) {
          // 获取用户信息
          const userInfoSuccess = await userStore.fetchUserInfo();
          if (userInfoSuccess) {
            ElMessage.success('登录成功');
            // 跳转到首页
            router.push('/');
          } else {
            userStore.logout();
            ElMessage.error('获取用户信息失败');
          }
        } else {
          ElMessage.error('用户名或密码错误');
          // 登录失败后显示验证码
          showCaptcha.value = true;
          refreshCaptcha();
        }
      } catch (error) {
        console.error('登录失败', error);
        ElMessage.error('登录失败，请稍后重试');
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 24px;
  color: #409eff;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
}

.captcha-container {
  display: flex;
  align-items: center;
}

.captcha-image {
  margin-left: 10px;
  height: 40px;
  cursor: pointer;
}
</style>
